home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8262 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: Sat, 02 Mar 96 16:16:27 GMT
  6. Organization: none
  7. Message-ID: <825783387snz@genesis.demon.co.uk>
  8. References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net> <4h8bud$1vd@castle.nando.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4h8bud$1vd@castle.nando.net>
  15.            actuary@nando.net "Bill McCarthy" writes:
  16.  
  17. >In <4h0j9e$ng5@clarknet.clark.net>,
  18. >yom@clark.net (yom) writes:
  19. >
  20. >>Since you're trying to sort a char**, the qsort function call must
  21. >>look like this:
  22. >>
  23. >>qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
  24. >>
  25. >>And your compare function must be defined like this:
  26. >>
  27. >>int compare(char **a,char **b) {return strcmp(*a,*b);}
  28. >
  29. >Didn't you mean to type "sizeof( char * )" which, IMHO, could be
  30. >better expressed as "sizeof array[0]" ?  Also, there's no need to
  31. >further complicate the call of qsort with the cast on compare if
  32. >you define compare() as:
  33.  
  34. The code as written requires a diagnostic from the compiler (if stdlib.h
  35. is included) because the comparison function type takes const void * arguments
  36. and  int (*)(void *, void *)  is incompatible with
  37. int (*)(const void *, const void *).
  38.  
  39. Even if the cast is fixed the code results in undefined behaviour since
  40. an int (char **, char **) function can't be legally called as an
  41. int (const void *, const void *) function which is how qsort() will call it.
  42.  
  43. >int compare( const void *a, const void *b )
  44. >{
  45. >   return strcmp( *(const char **)a, *(const char **)b );
  46. >}
  47.  
  48. IMHO it is desirable to maintain corresponding consts where possible when
  49. casting so better would be:
  50.  
  51.     return strcmp( *(char *const *)a, *(char *const *)b );
  52.  
  53. or
  54.  
  55.     return strcmp( *(const char *const *)a, *(const char *const *)b );
  56.  
  57. which corresponds to writing it without casts as:
  58.  
  59. int compare( const void *a, const void *b )
  60. {
  61.    const char *const *a2 = a;
  62.    const char *const *b2 = b;
  63.  
  64.    return strcmp( *a2, *b2 );
  65. }
  66.  
  67. If nothing else it avoids compiler warnings such as:
  68.   warning: cast discards `const' from pointer target type
  69.  
  70. gcc agrees with me! :-)
  71.  
  72. -- 
  73. -----------------------------------------
  74. Lawrence Kirby | fred@genesis.demon.co.uk
  75. Wilts, England | 70734.126@compuserve.com
  76. -----------------------------------------
  77.